package echo.netty.future_promise;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

/**
 * @author： zhangnan
 * @date： 2025/5/7 16:25
 * @description： TODO
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
public class TestNettyFuture {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Future<Integer> future = group.next().submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.debug("执行计算");
                Thread.sleep(1000);
                return 70;
            }
        });
        log.debug("等待结果");
//        log.debug("结果是 {}",future.get()); // 同步方式
        future.addListener(new GenericFutureListener<Future<? super Integer>>(){

            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.debug("结果是 {}",future.getNow()); // 或用get()都可以
            }
        });
    }

}
